package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.ZorderDao;
import cn.gson.fengboot.model.dao.ZreturnedgoodDao;
import cn.gson.fengboot.model.dao.ZreturnedmoneyDao;
import cn.gson.fengboot.model.dao.ZreturnedmoneylogDao;
import cn.gson.fengboot.model.mapper.ZreturnedmoneyMapper;
import cn.gson.fengboot.model.mapper.ZreturnedmoneylogMapper;
import cn.gson.fengboot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class ZreturnedmoneylogService {
    @Autowired
    ZreturnedmoneylogMapper hkjlmapper;

    @Autowired
    ZreturnedmoneylogDao dao;

    @Autowired
    ZreturnedmoneyDao jhdao;

    @Autowired
    ZorderDao dddao;

    @Autowired
    ZreturnedgoodDao thdao;
    public List<Zreturnedmoneylog> hkjllist(String search,String search2){
        return hkjlmapper.hkjllist(search,search2);
    }

    public List<Zreturnedmoneylog> hkjllist2(String search,String search2){
        return hkjlmapper.hkjllist2(search,search2);
    }

    public List<Zreturnedmoneylog> jhjl(Integer hkid){
        return hkjlmapper.jhjl(hkid);
    }

    /*
    回款记录页面新增方法
    */
    public void addfapiao(Zreturnedmoneylog jl){
        Zreturnedmoneylog save = dao.save(jl);
        Zreturnedmoney jh = jhdao.findById(save.getReturnedmoneyId().getReturnedmoneyId()).get();
        jh.setReturnedmoneyRetuen("是");
        jhdao.save(jh);

        if(jh.getOrderId().getOrderId()!=-1){
            Zorder zorder = dddao.findById(jh.getOrderId().getOrderId()).get();
            Integer sfhk = hkjlmapper.sfhk(jh.getOrderId().getOrderId());
            Integer sfjf = hkjlmapper.sfjf(jh.getOrderId().getOrderId())-1;
            if(sfhk==0 &&sfjf==0){
                zorder.setOrderState("已完成");
            }else {
                zorder.setOrderState("执行中");
            }
            dddao.save(zorder);
        }
        if (jh.getReturgoodId().getReturgoodId()!=-1){
            Zreturnedgood zreturnedgood = thdao.findById(jh.getReturgoodId().getReturgoodId()).get();
            Integer sftk = hkjlmapper.sftk(jh.getReturgoodId().getReturgoodId())-1;
            System.out.println(sftk);
            if(sftk==0){
                zreturnedgood.setReturgoodState("已完成");
                thdao.save(zreturnedgood);
            }
        }
    }

    //<!--谭方圆=>客户合并的回款记录查询-->
    public List<Zreturnedmoneylog> tfyhueikuanjl(Integer id1, Integer id2){
        return hkjlmapper.tfyhueikuanjl(id1,id2);
    }
}
